window.onload=function () {
    fullTable(bookList);
}
function fullTable(array) {
    let str="";
    array.forEach(n=>{
        str+=`<tr>    
                    <td><img src="${n.coverPhoto}" style="width: 50px;height: 50px"></td>
                    <td>${n.bookName}</td>
                    <td>${n.author}</td><td>${n.price}</td><td>${n.publicationDate}</td>
                    <td><input type="button" value="查看" onclick="checkInfo(${n.code})">
                    <input type="button" value="加入购物车" onclick="findById(${n.code})"></td>
                    </tr>`;
    })
    $("data").innerHTML=str;
}
function $(id) {
    return document.getElementById(id);
}
function showAddDiv() {
    $("addDiv").style.visibility="visible";
}

//查看详细内容
function checkInfo(code) {
    let emObj=bookList.find(n=>n.code==code);
    $("bookNameSpan").innerHTML=emObj.bookName;
    $("authorSpan").innerHTML=emObj.author;
    $("pDateSpan").innerHTML=emObj.publicationDate;
    $("priceSpan").innerHTML=emObj.price;
    $("imgSpan").src=emObj.coverPhoto;
    $("mainLeft").style.visibility="visible";
}
function findById(code){
    let emObj=bookList.find(n=>n.code==code)
    $("bookNameSpan").innerHTML=emObj.bookName;
    $("priceSpan").innerHTML=emObj.price;
    // $("numSpan").value=1;
    // $("totalPrice").innerHTML=(emObj.price)*num;
    $("addDiv").style.visibility="visible";
}
function findByName() {
    let nameStr=$("nameTxt").value;
    let newList=bookList.filter(n=>n.bookName.indexOf(nameStr)!=-1);
    fullTable(newList);
}
function addToShoppingCart() {
    let bookObj = bookList.find(n => n.code == code);
    if (!bookStoreList.find(n => n.code == code)) {
        bookStoreList.push({code: bookObj.code, bookName: bookObj.bookName, price: bookObj.price,num:1})
        buyTable(bookStoreList);
    }else {
        let bookObj = bookStoreList.find(n => n.code==code);
        bookObj.num ++;
        buyTable(bookStoreList);
    }
    $("totalPrice").innerHTML = totalPrice()+"元";
    $("shopDiv").style.visibility = "visible";
}
function buyTable(buyArray,num) {
    let str="";
    buyArray.forEach(n=>{
        str+=`<tr> 
                    <td>${n.bookName}</td><td>${n.price}</td>
                    <td><input type="text" id="count" value="${n.num}" onblur="numTxt(${n.code})"> </td>
                    <td>${n.price*n.num}</td>
                    <td><input type="button" value="移除商品" onclick="del(${n.code})"></td>
                    </tr>`;
    })
    $("data").innerHTML=str;
}
function buyMore(){
    $("addDiv").style.visibility="hidden";
}
function totalPrice(){
    let price = 0;
    bookStoreList.forEach(n =>{
        price += n.price*n.num;
    });
    return price;
}
function numTxt(id){
    bookStoreList.find(n => n.code == code).num = $("count").value;
    buyTable(bookStoreList);
    $("totalPrice").innerHTML = totalPrice()+"元";
}
function remove(code){
    let index = bookStoreList.findIndex(n => n.code==code);
    bookStoreList.splice(index,1);
    buyTable(bookStoreList);
    $("totalPrice").innerHTML = totalPrice()+"元";
}

